package com.ls.util;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ls
 * @version v1.0
 * @description
 * @date 2021/8/23 17:28
 */
public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    private static final String CODE = "code";
    private static final String MSG = "msg";

    private R() {
    }

    public static R error() {
        return error(BizCodeEnum.UNKNOWN_EXCEPTION);
    }

    public static R error(String msg) {
        return error(BizCodeEnum.UNKNOWN_EXCEPTION.getCode(), msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put(CODE, code);
        r.put(MSG, msg);
        return r;
    }

    public static R error(BizCodeEnum bizCodeEnum) {
        R r = new R();
        r.put(CODE, bizCodeEnum.getCode());
        r.put(MSG, bizCodeEnum.getMsg());
        return r;
    }


    public static R ok(String msg) {
        R r = new R();
        r.put(CODE, BizCodeEnum.SUCCESS.getCode());
        r.put(MSG, msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = ok();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return ok(BizCodeEnum.SUCCESS.getMsg());
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public Integer getCode() {
        return (Integer) this.get(CODE);
    }

}
